package com.lovo.bbs.controller;

import com.lovo.bbs.bean.Student;
import com.lovo.bbs.bean.form.StudentForm;
import com.lovo.bbs.result.ResponseResult;
import com.lovo.bbs.result.UserValid;
import com.lovo.bbs.service.StudentService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
@Tag(name = "学生管理")
@ResponseResult
@UserValid
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Object listStudent(StudentForm studentForm){
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);
        return studentService.listStudent(studentForm.getCurrent(),studentForm.getPageSize(),student);
    }
    @GetMapping("/{id}")
    public Object getById(@PathVariable Integer id){
        return studentService.getById(id);
    }
    @PostMapping
    public Object save(@RequestBody StudentForm studentForm){
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);
        return studentService.save(student);
    }
    @PutMapping("/{id}")
    public Object update(@PathVariable Integer id,@RequestBody StudentForm studentForm){
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);
        student.setId(id);
        return studentService.updateById(student);
    }

    @DeleteMapping("/{id}")
    public Object delete(@PathVariable("id") Integer id){
        return studentService.removeById(id);
    }
}
